package com.yazdankhah.listeners;

import com.yazdankhah.application.Const;
import com.yazdankhah.utils.TextDataTools;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;


/**
 *
 * @author ahmad
 */
public class ContextListener implements ServletContextListener {
  
  private static Logger logger = null; // initialize after DOMConfigurator.configure() is called.
  private ServletContext context = null;


  @Override
  public void contextInitialized(ServletContextEvent event) {
    
    context = event.getServletContext();
        
    String rootRealPath = context.getRealPath("/");
    System.out.println("rootRealPath = [" + rootRealPath + "]");
    
    String log4jFile = rootRealPath + Const.LOG_HOME + "log4j.xml";
    
    DOMConfigurator.configureAndWatch(log4jFile, 300000);
    
    logger = Logger.getLogger(ContextListener.class);
    logger.info("qtt logging started and Log4J configured using: " + log4jFile);
    
    TextDataTools.loadConfig(rootRealPath);
    
    String sqlFN = TextDataTools.lookupConfig(Const.ContextKeys.APP_DATABASE) + ".properties";
    TextDataTools.loadSqls(rootRealPath, sqlFN);
    
    TextDataTools.loadMyQuran(rootRealPath);
    
    
//    System.out.println("(1,7) = [" + TextDataTools.lookupQuranVerse(1, 7) + "]");
//    System.out.println("(2,1) = [" + TextDataTools.lookupQuranVerse(2, 1) + "]");
//    System.out.println("(1,3) = [" + TextDataTools.lookupQuranVerse(1, 3) + "]");
    
  } //method
  
  


  @Override
  public void contextDestroyed(ServletContextEvent event) {
    
    context = event.getServletContext();
    context = null;
  } //method
  
} //class
